home *** CD-ROM | disk | FTP | other *** search
- /*
- * VirtuaLight's binary .VIB format API, sample 4
- * Written by Stephane Marty, 09/10/2001
- *
- * This sample program writes a binary VirtuaLight MSH file
- * describing a procedural spring mesh.
- */
-
- #include "..\vlBinDef.h"
-
- #define TWO_PI (6.283185307179586476925287)
-
- #define NU 180
- #define NV 24
- #define R1 0.5
- #define R2 0.2
- #define PERIOD 2
- #define CYCLES 5
-
- static viVECTOR
- normal(viVECTOR v, viVECTOR v1, viVECTOR v2)
- {
- viVECTOR n, va, vb;
-
- viVecSubstract(va, v1, v);
- viVecNormalize(va);
- viVecSubstract(vb, v2, v);
- viVecNormalize(vb);
- viVecCross(n, va, vb);
- viVecNormalize(n);
- return(n);
- }
-
- static viVECTOR
- evaluate(double u, double v)
- {
- viVECTOR q;
-
- viSetDbl(q.x, (1.0 - R1 * cos(v)) * cos(u));
- viSetDbl(q.y, (1.0 - R1 * cos(v)) * sin(u));
- viSetDbl(q.z, R2 * (sin(v) + u * PERIOD / M_PI));
- return(q);
- }
-
- void main(void)
- {
- unsigned long pri=0;
- int i, j;
- double u, v, du, dv;
- viVECTOR q[4], n[4], vec;
- viPATCH tri;
-
- du = CYCLES * TWO_PI / (double)NU;
- dv = TWO_PI / (double)NV;
-
- // Create a new MSH file
- if(viNewMSHFile("sample4.msh") != TRUE)
- {
- fprintf(stderr, "\nImpossible to create the file.\n");
- exit(-1);
- }
-
- // Create and declare the procedural object named "smooth_spring"
- viStartMeshDeclaration("smooth_spring");
- for (i=0; i<NU; i++)
- {
- u = i * du;
- for (j=0; j<NV; j++)
- {
- v = j * dv;
- // this object is a set of polygons, but each of them
- // is divided in two triangular patches.
- q[0] = evaluate(u, v);
- n[0] = normal(q[0], evaluate(u+du/10, v), evaluate(u, v+dv/10));
- q[1] = evaluate(u+du, v);
- n[1] = normal(q[1], evaluate(u+du+du/10, v), evaluate(u+du, v+dv/10));
- q[2] = evaluate(u+du, v+dv);
- n[2] = normal(q[2], evaluate(u+du+du/10, v+dv), evaluate(u+du, v+dv+dv/10));
- q[3] = evaluate(u, v+dv);
- n[3] = normal(q[3], evaluate(u+du/10, v+dv), evaluate(u, v+dv+dv/10));
- // set and dump the first triangular patch...
- viCopyVector(&tri.v1, &q[0]);
- viCopyVector(&tri.n1, &n[0]);
- viCopyVector(&tri.v2, &q[1]);
- viCopyVector(&tri.n2, &n[1]);
- viCopyVector(&tri.v3, &q[2]);
- viCopyVector(&tri.n3, &n[2]);
- viAddPatchToMesh(&tri);
- // ...and the second one (first vertex doesn't change).
- viCopyVector(&tri.v2, &q[2]);
- viCopyVector(&tri.n2, &n[2]);
- viCopyVector(&tri.v3, &q[3]);
- viCopyVector(&tri.n3, &n[3]);
- viAddPatchToMesh(&tri);
- pri += 2;
- }
- }
- viEndMeshDeclaration();
-
- // The following lines invoke (call) the mesh
- // from inside the MSH file.
- // If you rather prefer call it from the ASCII VIB
- // scene, remove them and add the following to the VIB file:
- // smooth_spring [ Rotate(0,0,45) smooth_spring_shader ]
- viCallMesh("smooth_spring");
- viSetMeshShader("smooth_spring_shader");
- viRotateMesh(viSetVector(&vec, 0, 0, 45));
- viEndMesh();
-
- // Close the MSH file
- (void)viCloseMSHFile();
-
- fprintf(stderr, "\n%lu patches contained into the MSH file.\n", pri);
- }